package ch04.Use;

/**
 * @author chenmin
 * @desc TODO
 * @date 2025/9/4
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class LoginTest {
    public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        // 向集合中添加5个用户对象
        userList.add(new User("lajiao", "123456"));
        userList.add(new User("xiaomei", "7891011"));
        userList.add(new User("Nekko", "13579"));
        userList.add(new User("mushu", "246810"));
        userList.add(new User("chaorou", "zxcvbn"));

        Scanner scanner = new Scanner(System.in);
        int chance = 3; // 三次输入机会

        while (chance > 0) {
            System.out.print("请输入用户名：");
            String inputUsername = scanner.nextLine();
            System.out.print("请输入密码：");
            String inputPassword = scanner.nextLine();

            boolean isSuccess = false;
            // 遍历集合，与用户输入的用户名和密码比较
            for (User user : userList) {
                if (user.getUsername().equals(inputUsername) && user.getPassword().equals(inputPassword)) {
                    isSuccess = true;
                    break;
                }
            }

            if (isSuccess) {
                System.out.println("登录成功！");
                break;
            } else {
                chance--;
                System.out.println("用户名或密码错误，还剩 " + chance + " 次机会。");
            }
        }

        if (chance == 0) {
            System.out.println("输入次数已用完，登录失败！");
        }

        scanner.close();
    }
}
